package com.lframework.xingyun.settle.api;

import com.lframework.starter.web.resp.InvokeResult;
import com.lframework.starter.web.resp.InvokeResultBuilder;
import com.lframework.xingyun.settle.api.vo.CreateSettleCheckSheetApiVo;
import com.lframework.xingyun.settle.api.vo.SettleCheckSheetItemApiVo;
import com.lframework.xingyun.settle.service.SettleCheckSheetService;
import com.lframework.xingyun.settle.vo.check.CreateSettleCheckSheetVo;
import com.lframework.xingyun.settle.vo.check.SettleCheckSheetItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 供应商对账单-供内部模块调用API实现类
 * @author: zhuchi
 * @create: 2023-11-04 20:25
 * @Version 1.0
 **/
@Service
public class SettleCheckSheetApiImpl implements SettleCheckSheetApi{

    @Autowired
    private SettleCheckSheetService settleCheckSheetService;

    /**
     * 创建供应商对账单
     */
    public InvokeResult<String> create(@RequestBody @Valid CreateSettleCheckSheetApiVo vo){
        vo.validate();
        CreateSettleCheckSheetVo createSettleCheckSheetVo = new CreateSettleCheckSheetVo();
        BeanUtils.copyProperties(vo, createSettleCheckSheetVo);

        List<SettleCheckSheetItemVo> items = new ArrayList<>();
        for (SettleCheckSheetItemApiVo itemApiVo : vo.getItems()){
            SettleCheckSheetItemVo settleCheckSheetItemVo = new SettleCheckSheetItemVo();
            BeanUtils.copyProperties(itemApiVo, settleCheckSheetItemVo);
            items.add(settleCheckSheetItemVo);
        }
        createSettleCheckSheetVo.setItems(items);
        String id = settleCheckSheetService.create(createSettleCheckSheetVo);

        return InvokeResultBuilder.success(id);
    }

}
